home *** CD-ROM | disk | FTP | other *** search
-
- #ifndef __AUDIOENGINE_H_
- #define __AUDIOENGINE_H_
- /*
- Peon - Win32 Games Programming Library
- Copyright (C) 2002-2005 Erik Yuzwa
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public
- License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Library General Public License for more details.
-
- You should have received a copy of the GNU Library General Public
- License along with this library; if not, write to the Free
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- Erik Yuzwa
- peon AT wazooinc DOT com
- */
-
- #include "ISingleton.h"
-
-
- #define NUM_BUFFERS 30
-
- #define OGG_BUFFER_SIZE (4096 * 8)
-
- namespace peon
- {
- /**
- * This structure is responsible for encapsulating a 3D sound
- * within our game world. Hopefully it's fairly generic enough
- * to handle most situations. These nodes are really only meant
- * for OGG and WAV files (pretty much anything supported by OpenAL).
- * For MIDI audio, you should be looking at the loadMidi method
- * of the AudioEngine object.
- */
- struct PEONMAIN_API AudioNode
- {
- /** the source buffer */
- ALuint sound_source;
-
- int sound_buffer;
-
- /** loop the sound? */
- bool sound_loop;
-
- /** sound's position in 3D space */
- ALfloat sound_position[3];
-
- /** sound's velocity within the game world */
- ALfloat sound_velocity[3];
-
- public:
- /**
- * Constructor
- */
- AudioNode()
- {
- sound_source = -1;
- sound_buffer = -1;
- sound_loop = false;
- sound_position[0] = 0.0f;
- sound_position[1] = 0.0f;
- sound_position[2] = 0.0f;
-
- sound_velocity[0] = 0.0f;
- sound_velocity[1] = 0.0f;
- sound_velocity[2] = 0.0f;
- }
-
-
- };
-
-
- /**
- * This object is used to encapsulate our audio hardware. It should
- * be able to play both MIDI tunes (yes people still use 'em) and
- * OGG/WAV files.
- *
- */
- class PEONMAIN_API AudioEngine : public ISingleton<AudioEngine>
- {
-
- protected:
- /** The OpenAL context */
- ALCcontext *m_pALContext;
-
- /** The OpenAL device */
- ALCdevice *m_pALDevice;
-
- /** do we enable/disable sound? Note this only affects OGG and WAV playback */
- bool m_bEnableSound;
-
- /** do we enable/disable music? Note this only affects MIDI playback */
- bool m_bEnableMusic;
-
- /** a storage buffer for NUM_BUFFERS number of sounds */
- ALuint m_uAudioBuffers[ NUM_BUFFERS ];
-
- /** is our audio hardware even supported? */
- bool m_bAudioSupported;
-
- int m_iCurrentSlot;
-
-
- public:
- /**
- * Constructor
- */
- AudioEngine();
-
- /**
- * Destructor
- */
- ~AudioEngine();
-
- /** Override standard Singleton retrieval.
- @remarks
- Why do we do this? Well, it's because the Singleton
- implementation is in a .h file, which means it gets compiled
- into anybody who includes it. This is needed for the
- Singleton template to work, but we actually only want it
- compiled into the implementation of the class based on the
- Singleton, not all of them. If we don't change this, we get
- link errors when trying to use the Singleton-based class from
- an outside dll.
- @par
- This method just delegates to the template version anyway,
- but the implementation stays in this single compilation unit,
- preventing link errors.
- */
- static AudioEngine& getSingleton(void);
- /** Override standard Singleton retrieval.
- @remarks
- Why do we do this? Well, it's because the Singleton
- implementation is in a .h file, which means it gets compiled
- into anybody who includes it. This is needed for the
- Singleton template to work, but we actually only want it
- compiled into the implementation of the class based on the
- Singleton, not all of them. If we don't change this, we get
- link errors when trying to use the Singleton-based class from
- an outside dll.
- @par
- This method just delegates to the template version anyway,
- but the implementation stays in this single compilation unit,
- preventing link errors.
- */
- static AudioEngine* getSingletonPtr(void);
-
-
- /**
- * This method loads and instantiates the subsystems necessary in
- * SDL_Mixer and OpenAL to get a device working for each. If the
- * initialization fails, then you can decide if you want to just
- * disable all sound, or quit the app entirely.
- * @param pConfig - The INI information
- * @return bool - true if audio loaded properly
- */
- bool loadEngine( IniConfigReader* pConfig );
-
- /**
- * This method frees up our allocated audio resources
- */
- void unloadEngine();
-
- /**
- * This method makes the necessary calls to load up a
- * Mix_Music instance which is used for playback of
- * MIDI files
- * @param strFilename - path to the MIDI file
- * @return Mix_Music* - pointer to our Mix_Music object
- */
- Mix_Music* loadMIDI( const String& strFilename );
-
- /**
- * This method makes the necessary calls to load up a
- * Mix_Chunk instance which is used for playback of
- * MIDI files
- * @param strFilename - path to the WAV file
- * @return Mix_Music* - pointer to our Mix_Chunk object
- */
- Mix_Chunk* loadWAVChunk( const String& strFilename );
-
-
- /**
- * This method internally loads the audio resource
- * into some OpenAL compatible buffers. When you wish
- * to work with a resource, you need to reference it by
- * the slot you stored it in.
- * @param strFilename - path to WAV file
- * @param slot - slot to store resource
- * @return bool - true if sound loaded properly
- */
- bool loadAudioNode( const String& strWAVFile, AudioNode* pNode );
-
- /**
- * This method is responsible for setting the current
- * AudioNode
- * @param pNode - pointer to valid AudioNode
- */
- void setAudioNode( AudioNode* pNode );
-
- /**
- * This method is responsible for playing the currently
- * set AudioNode
- * @param pNode - pointer to a valid AudioNode
- */
- void playAudioNode( AudioNode* pNode );
-
- /**
- * This method is responsible for stopping the playback
- * of the AudioNode
- * @param pNode - pointer to AudioNode to stop
- */
- void stopAudioNode( AudioNode* pNode );
-
- /**
- * This method just enables the global audio mask
- */
- void enableSound(){ m_bEnableSound = true; }
-
- /**
- * This method disables the global audio mask
- */
- void disableSound(){ m_bEnableSound = false; }
-
- /**
- * This method enables the global music mask
- */
- void enableMusic(){ m_bEnableMusic = true; }
-
- /**
- * This method disables the global music mask
- */
- void disableMusic(){ m_bEnableMusic = false; }
-
- };
- }
-
- #endif
-